package com.scau.os.MemorySys;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import com.scau.os.MemorySys.Test.*;

public class Windows extends Application{
    public  static Memory memory =new Memory(512);


    List<Hole> hole_list = new LinkedList<>();
    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage stage) throws Exception {
        Test t = new Test();
        t.allocate(memory);

        stage.setTitle("TEST");

        Canvas canvas = new Canvas(500,100);
        GraphicsContext gs = canvas.getGraphicsContext2D();
        ArrayList<Color> colorList = new ArrayList<>(30);
        for (int i = 0 ;i<30;i++){
            double red = Math.random()*100;
            double blue = Math.random()*100;
            double green = Math.random()*100;
            double opacityRandom = Math.random()*100;
            Color c = new Color(red/100,blue/100,green/100,opacityRandom/100);
            if (c != Color.WHITE&& c !=Color.LIGHTBLUE){

                colorList.add(c);
            }
        }
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                hole_list = memory.getHoles();
                paint(gs,hole_list,colorList);
            }
        }
        );
        thread.start();
        Group group = new Group();
        group.getChildren().add(canvas);
        stage.setScene(new Scene(group));
        stage.show();
    }

    private void paint(GraphicsContext gs, List<Hole> list, ArrayList<Color> colorArrayList){

        int m = 0,size_new,size_old=0,width_old = 0;
        int i = list.size();
        int width = 25;
        gs.setFill(Color.LIGHTBLUE);
        gs.fillRect(10,10,430,50);
        gs.setFill(Color.WHITE);
//        gs.fillArc(20,20, 10,30,90,180, ArcType.OPEN);
        gs.fillRect(20,20,400,30);
//        gs.fillArc(20+400,20, 10,30,270,180, ArcType.OPEN);
        for (Hole hole:list){

            if (m!= i-1) {
                gs.setFill(colorArrayList.get(m));
                size_new = hole.getSize();
                gs.fillRect(width_old+20, 20, width + size_new/5, 30);
                width_old += width + size_new/5;
                gs.setFill(Color.BLACK);
                gs.fillText(String.valueOf(hole.getSize()), width_old-size_new/5, 20 + 20);
                size_old = hole.getSize();
            }
            m++;
        }

    }
}
